﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SchoolManagementSystem.Controls;
using CoreData;
namespace SchoolManagementSystem.Searchs
{
    public partial class TempStudentSearchUC : SearchUC
    {
        public TempStudentSearchUC()
        {
            InitializeComponent();
          //  LoadDataCode();
            //LoadDataName();
            LoadDataStatus();
            LoadDataChannel();
            DateEdit_contactfrom.DateTime = new DateTime(DateTime.Now.Year - 1, 1, 1);
            dateEdit_contactto.DateTime = DateTime.Now;
        }

        void LoadDataCode()
        {
            try
            {
                var data = (List<TempStudent>)RegisterService.RegisterServices.TempStudentService.GetList();
                data.Insert(0, new TempStudent()
                {
                    Active = true,
                    Code = "Tất cả",
                    Name = "Tất cả",
                    ID = 0
                });

                lookUpEdit_Code.Properties.DataSource = data.OrderBy(c => c.ID);
                lookUpEdit_Code.Properties.ValueMember = "Code";
                lookUpEdit_Code.Properties.DisplayMember = "Code";
                string[] columns = { "Code" };
                for (int i = 0; i < lookUpEdit_Code.Properties.Columns.Count; i++)
                {
                    {
                        var index = Array.IndexOf(columns, lookUpEdit_Code.Properties.Columns[i].FieldName);
                        if (index < 0)
                        {
                            lookUpEdit_Code.Properties.Columns[i].Visible = false;
                            lookUpEdit_Code.Properties.Columns[i].AllowSort = DevExpress.Utils.DefaultBoolean.True;

                        }
                    }
                }
            }
            catch { }
        }

        void LoadDataName()
        {
            try
            {
                var data = (List<TempStudent>)RegisterService.RegisterServices.TempStudentService.GetList();
                data.Insert(0, new TempStudent()
                {
                    Active = true,
                    Code = "Tất cả",
                    Name = "Tất cả",
                    ID = 0
                });

                lookUpEdit_Name.Properties.DataSource = data.OrderBy(c => c.ID);
                lookUpEdit_Name.Properties.ValueMember = "Name";
                lookUpEdit_Name.Properties.DisplayMember = "Name";
                string[] columns = { "Name" };
                for (int i = 0; i < lookUpEdit_Name.Properties.Columns.Count; i++)
                {
                    {
                        var index = Array.IndexOf(columns, lookUpEdit_Name.Properties.Columns[i].FieldName);
                        if (index < 0)
                        {
                            lookUpEdit_Name.Properties.Columns[i].Visible = false;
                            lookUpEdit_Name.Properties.Columns[i].AllowSort = DevExpress.Utils.DefaultBoolean.True;

                        }
                    }
                }
            }
            catch { }
        }
        void LoadDataStatus()
        {
            var data = (List<Student_Status>)RegisterService.RegisterServices.StudentStatusService.GetList(" Type =2");
            data.Insert(0, new Student_Status()
            {
                Active = true,
                Code = "Tất cả",
                Name = "Tất cả",
                ID = 0
            });
            lookUpEdit_status.Properties.DataSource = data.OrderBy(c => c.ID);
            lookUpEdit_status.Properties.ValueMember = "ID";
            lookUpEdit_status.Properties.DisplayMember = "Name";
            string[] columns = { "Code","Name" };
            for (int i = 0; i < lookUpEdit_status.Properties.Columns.Count; i++)
            {
                {
                    var index = Array.IndexOf(columns, lookUpEdit_status.Properties.Columns[i].FieldName);
                    if (index < 0)
                    {
                        lookUpEdit_status.Properties.Columns[i].Visible = false;
                        lookUpEdit_status.Properties.Columns[i].AllowSort = DevExpress.Utils.DefaultBoolean.True;

                    }
                }
            }
        }


        void LoadDataChannel()
        {
            var data =  (List<ChannelInfo>)RegisterService.RegisterServices.ChannelInfoService.GetList();
           data.Insert(0, new ChannelInfo(){
                Active=true,
                Code="Tất cả",
                Name="Tất cả",
                ID =0
           });
           lookUpEdit_Channel.Properties.DataSource = data.OrderBy(c => c.ID);
            lookUpEdit_Channel.Properties.ValueMember = "ID";
            lookUpEdit_Channel.Properties.DisplayMember = "Name";
            string[] columns = { "Code", "Name" };
            for (int i = 0; i < lookUpEdit_Channel.Properties.Columns.Count; i++)
            {
                {
                    var index = Array.IndexOf(columns, lookUpEdit_Channel.Properties.Columns[i].FieldName);
                    if (index < 0)
                    {
                        lookUpEdit_Channel.Properties.Columns[i].Visible = false;
                        lookUpEdit_Channel.Properties.Columns[i].AllowSort = DevExpress.Utils.DefaultBoolean.True;

                    }
                }
            }
        }

        private void cmdtim_Click(object sender, EventArgs e)
        {
            string search ="";
            var code = lookUpEdit_Code.EditValue!=null ? lookUpEdit_Code.EditValue.ToString():"";
            var name = lookUpEdit_Name.EditValue != null ? lookUpEdit_Name.EditValue.ToString() : "";
            var datefrom = DateEdit_contactfrom.DateTime;
            var dateto = dateEdit_contactto.DateTime;
            long status =0;
            long channel =0;
            if (lookUpEdit_status.EditValue != null)
            {
                status = (long)(lookUpEdit_status.EditValue);
            }
            if (lookUpEdit_Channel.EditValue != null)
            {
              channel =  (long)(lookUpEdit_Channel.EditValue);
            }
          
            if (!String.IsNullOrEmpty(code) && code != "Tất cả")
            {
                search += " TempStudent.Code= N'" + code + "' and";
            }
            if (!String.IsNullOrEmpty(name) && name != "Tất cả")
            {
                search += " TempStudent.Name= N'" + name + "' and";
            }
            if (datefrom > dateto)
            {
                MessageControl.ShowMessage("Ngày từ phải nhỏ hơn hoặc bằng ngày đến", "Error", MessageControl.Icon.Error);
                return;
            }
            //convert cho ra ket qua la yyyy-MM-DD
            search += " Convert(varchar(11), TempStudent.ContactDate, 120) BETWEEN '" + datefrom.ToString("yyyy-MM-dd") + "' and '" + dateto.ToString("yyyy-MM-dd") + "' and";
            if (status > 0)
            {
                search += " TempStudent.Status =" + status + " and";
            }
            if (channel > 0)
            {
                search += " TempStudent.ChannelInfoID =" + channel + " and";
            }

            //if (radioGroup_CourseType.SelectedIndex != 0)
            //{
            //    search += " TempStudent.ChannelInfoID =" + channel + " and";
            //}

            search = search.Remove(search.Length - 3, 3);
            Master.SearchString = search;
         
        }

        private void lookUpEdit_Code_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            LoadDataCode();
            lookUpEdit_Code.ShowPopup();
        }

        private void lookUpEdit_Name_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            LoadDataName();
            lookUpEdit_Name.ShowPopup();
        }

        private void lookUpEdit_Code_Click(object sender, EventArgs e)
        {
            LoadDataCode(); lookUpEdit_Code.ShowPopup();
        }

        private void lookUpEdit_Name_Click(object sender, EventArgs e)
        {
            LoadDataName(); lookUpEdit_Name.ShowPopup();
        }

        private void simpleButton_close_Click(object sender, EventArgs e)
        {
            ClosePanel();
        }

    }
}
